﻿var expDate = new Date();
expDate.setTime(expDate.getTime() - (1 * 24 * 60 * 60 * 1000)); // 减 1 days
$.cookie("user", '', { expires: expDate });
localStorage.setItem('loginUser', '');

$('#_rmnTxt').click(function () {
    var _status = $('#_rmn').css('display');
    if (_status == 'none')
        $('#_rmn').css('display', 'inline');
    else
        $('#_rmn').css('display', 'none');
});

$("#_login").click(function () {
    login();
});

$(document).keydown(function (e) {
    if (e.keyCode == 13)
        login();
});
var b = new Base64();
let logining = false;

function logTip(_msg) {
    $('#_tip').html(_msg);
    setTimeout(() => {
        $('#_tip').html('');
        $("#_account").parent().removeClass('errorInput');
        $("#_password").parent().removeClass('errorInput');
    }, 3000);
}

//登录
function login() {
    if (logining) {
        logTip('操作太频繁');
        return;
    }
    var _arr = $.cookie('reqConfItem').split(',');
    if (_arr.length !== 2) {
        logTip('缺少配置参数');
        return;
    }
    var _appId = _arr[0];
    var _loginAccount = '';
    var _loginPassword = '';
    var _account = $.trim($("#_account").val());
    var _password = $.trim($("#_password").val());
    if (!_account) {
        $("#_account").parent().addClass('errorInput');
        logTip('请输入用户名');
        return;
    }
    _loginAccount = _account;
    if (!_password) {
        $("#_password").parent().addClass('errorInput');
        logTip('请输入密码');
        return;
    }
    _loginPassword = _password;
    _loginAccount = decodeAndMixStr(_loginAccount, 1);
    _loginPassword = decodeAndMixStr(_loginPassword, 2);
    var token = { appId: _appId, device_name: 'testPad', loginUserCode: _loginAccount, timeStamp: '', language: language, loginSysCode: 'ICM' }
    var encdeToken = b.encode(encodeURI(JSON.stringify(token)));
    var signData = { appId: token.appId, device_name: token.device_name, loginUserCode: token.loginUserCode, timeStamp: token.timeStamp, account: _loginAccount, password: _loginPassword, language: token.language, loginSysCode: token.loginSysCode };
    var sign = createSign(signData);
    logining = true;
    localStorage.setItem('loginUser',JSON.stringify({"userNo":_account,"timeStamp":""}));
    $("#_login").html($("#_login").html() + '...');
    $.ajax({
        url: apiUrl + '/GeneralLogin/Log_in',
        data: JSON.stringify({ account: _loginAccount, password: _loginPassword, sign: sign }),
        headers: { token: encdeToken },
        dataType: 'json',
        type: 'post',
        success: function (r) {
            logining = false;
            $("#_login").html($("#_login").html().replace('...', ''));
            if (r.error == 0) {
                var expDate = new Date();
                expDate.setTime(expDate.getTime() + (7 * 24 * 60 * 60 * 1000)); // add 7 days
                //存入用户账号和时间戳
                $.cookie("user", b.encode(JSON.stringify({ "userNo": r.data.user_no, "timeStamp": r.data.timeStamp })), { expires: expDate });
                localStorage.setItem('loginUser', JSON.stringify({ "userNo": r.data.user_no, "timeStamp": r.data.timeStamp }));
                console.log();
                location.href = 'index.html';
            }
            else {
                logTip(r.message);
            }
        }
    });
}
